package com.bajiao.admin.controller.account;

import com.bajiao.admin.base.AdminBaseController;
import com.bajiao.common.api.vo.Result;
import com.bajiao.common.system.vo.LoginUser;
import com.bajiao.entity.account.Account;
import com.bajiao.service.account.AccountService;
import com.bajiao.vo.account.AccountAdmin;
import com.bajiao.vo.user.UserAdmin;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 账户
 * @Author: FanJq
 * @Date: 2024-10-11
 * @Version: V1.0
 */
@Slf4j
@Api(tags = "账户")
@RestController
@RequestMapping("/account")
public class AccountController extends AdminBaseController<Account, AccountService> {

    @Resource
    private AccountService accountService;

    @ApiOperation(value = "账户-分页列表查询", notes = "账户-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(String keywords) {
        startPage();
        List<AccountAdmin> resultList = accountService.getAdminList(keywords);
        return Result.ok(getDataTable(resultList));
    }

    @ApiOperation(value = "账户-分页列表查询", notes = "账户-分页列表查询")
    @PostMapping(value = "/modify/money")
    public Result<?> modifyMoney(@RequestBody AccountAdmin request) {
        accountService.modifyMoney(request.getId(), request.getMoney());
        return Result.ok();
    }

    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(String keywords, String fileName) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        // Step.2 获取导出数据
        List<AccountAdmin> exportList = accountService.getAdminList(keywords);
        // Step.3 AutoPoi 导出Excel
        ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
        mv.addObject(NormalExcelConstants.FILE_NAME, fileName); //此处设置的filename无效 ,前端会重更新设置一下
        mv.addObject(NormalExcelConstants.CLASS, AccountAdmin.class);
        mv.addObject(NormalExcelConstants.PARAMS, new ExportParams(fileName, "导出人:" + sysUser.getRealname(), fileName));
        mv.addObject(NormalExcelConstants.DATA_LIST, exportList);
        return mv;
    }
}
